# -*- coding: utf-8 -*-

def menuindex(index):
	def handler_creator(view):
		def alt_handler(*view_args, **view_kargs):
			request = view_args[0] if view_args else None
			if request:
				request.menuindex = index
			return view(*view_args, **view_kargs)
		return alt_handler
	return handler_creator

def create_pagination(current=1, pagecount=0, has_more_pages=True):
	start = 1 if current<=4 else current-4
	nodes = []
	if start>1:
		nodes.append({'url':'', 'label':u'首页', 'is_link':True, 'index':1, 'css':'pre'})
	if current>1:
		nodes.append({'url':'', 'label':u'上一页', 'is_link':True, 'index':current-1, 'css':'pre'})
	else:
		nodes.append({'url':'', 'label':u'上一页', 'is_link':False, 'css':'pre'})
	shown_max_page = current+1 if has_more_pages else current
	for i in range(start, shown_max_page+1):
		if i==current:
			nodes.append({'url':'', 'label':u'%s' % i, 'is_link':False, 'index':i, 'current':True, 'css':'current'})
		else:
			nodes.append({'url':'', 'label':u'%s' % i, 'is_link':True, 'index':i})
	if has_more_pages:
		nodes.append({'url':'', 'label':u'下一页', 'is_link':True, 'index':current+1, 'css':'next'})
	else:
		nodes.append({'url':'', 'label':u'下一页', 'is_link':False, 'css':'next'})
	def set_url(node):
		if node.has_key('index'):
			node['url'] = '?page=%s' % node['index']
	map(set_url, nodes)
	return nodes